<?php
$ROOT_DIR_PATH = '../';
require_once($ROOT_DIR_PATH . 'includes/includes.php');
require_once($ROOT_DIR_PATH . 'classes/class.user.inc.php');
$loginUrl = $ADMIN_BASE_HREF . "login.php";
$dashBoardUrl = $ADMIN_BASE_HREF . "dashboard.php";
$ADMIN_PAGE_TITLE = 'Dealers listing';
 if (!$GENOBJ->isLoggedIn()) {
      $GENOBJ->redirect($loginUrl);
 }
 
 if(!$GENOBJ->isAdminLogin()) {
	$GENOBJ->redirect($dashBoardUrl);
}

$userObj = new user();
$succMessageArray = Array();
$errorMessageArray = Array();
/* This is for the error and success messages that came from content add page */
if (isset($_SESSION['error'])) {
    if (array_key_exists('user-error', $_SESSION['error'])) {
        $errorMessageArray = $_SESSION['error'];
    } else {
        $succMessageArray = $_SESSION['error'];
    }
}
$GENOBJ->clearSessionErrorMessages();
$action = '';
$filterArray = Array("TrimElement", "htmlEntity");
$where = '';

$searchtype = '';
$searchtext = "";
if ($_POST) {
    if (isset($_POST['multiaction']) && ($_POST['multiaction'] != '')) {
        $action = $_POST['multiaction'];
        $action = $GENOBJ->filter($action, $filterArray);
        if (isset($_POST['multicheck']) && count($_POST['multicheck']) > 0) {
            $ids = implode(",", $_POST['multicheck']);
            $result = $userObj->performListAction($action, $ids);
            if ($result) {
                if ($action == 'delete') {
                    $succMessageArray[] = 'Successfully Deleted';
                } else {
                    $succMessageArray[] = 'Successfully Updated';
                }
            } else {
                $errorMessageArray[] = 'There is some problem in ' . $action;
            }
        } else {
            $errorMessageArray[] = 'Please select atleast one checkbox';
        }
    }

    /* Search Facility Over Here */
    if (isset($_POST['searchtype']) && ($_POST['searchtype'] != '')) {
        $searchtype = $_POST['searchtype'];
        $searchtype = $GENOBJ->filter($searchtype, $filterArray);
        if ($searchtype == 'email' || $searchtype == 'username' || $searchtype == 'firstname' || $searchtype == 'lastname') {
            $searchtext = $_POST['searchtext'];
            $searchtext = $GENOBJ->filter($searchtext, $filterArray);
            $searchtext = $GENOBJ->searchfilter($searchtext);
            if ($GENOBJ->IsEmpty($searchtext)) {
                $errorMessageArray[] = 'Please enter search text for search.';
            } else {
                $where = "WHERE $searchtype LIKE '%$searchtext%'";
            }
        }
    } else {
        $where = " where 1 = 1";
    }
} else {
    $where = " where 1 = 1";
}
#echo $where ;
if (count($errorMessageArray) < 0) {
    $where = " where 1 = 1";
}

if (isset($_POST['showall']) && ($_POST['showall'] != '')) {
    $where = " where 1 = 1";
}

$where .= " and userid != '0'";
/* Paging logic over here */
$order = '`userid` ';
$params = 'userid,firstname,lastname,email,username,status';
$totalUser = $userObj->getCountUser( "where  userid != '0'");


/*Pagination */
$itemsPerPageReviews = PAGING_LIMIT; //limit
$currentPageReviews = 1;
if ($_GET) {
	if (isset($_GET['page']) && $_GET['page'] != "") {
	$currentPageReviews = $_GET['page'];
	}
}

$offset = ($currentPageReviews - 1) * $itemsPerPageReviews;
$paginator = new Paginator();
$paginator->setItemsPerPage($itemsPerPageReviews);
$paginator->setItemsTotal($totalUser);
$paginator->setCurrentPage($currentPageReviews);
$link = "userlisting.php?page=PAGENO";
$paginator->paginate($link);
$limit = " limit ".$itemsPerPageReviews." OFFSET ".$offset;



$userData = $userObj->getAllUserList($params, $where, $order, $limit);
recursivEncodeArray($userData); //Utf change
$searchtext = $GENOBJ->cleanfilter($searchtext);
?>
<?php include('templates/header.php');?>
<body>
    <div id="user-listing"></div>
	<div id="ncleangrey-container">
		<?php include('templates/navigation.php');?>
		<div class="breadcrumbs">
			<div id="nav-icons_all">
				<ul id="nav-icons">
					<li class="logout-icon"><a  title="Logout"  href="logout.php">Logout</a></li>
				</ul>
			</div>
			<p class="breadcrumbs"><a title="Home" href="dashboard.php">Home</a> &#187; Dealer </p>
		</div>
		<div class="hstippled">&nbsp;</div>
		<div id="MainContent">
               <div class="clearb">&nbsp;</div>

			<?php if(count($succMessageArray) > 0):?>
                        <div class="pagemcontainer">
                        <?php foreach($succMessageArray as $succValue):?>
                            <p class="pagemessage">
                                    <img src="images/icons/system/accept.gif" class="systemicon" alt="Success" title="Success" />
                                    <label class="errorlabel"><?php echo $succValue;?></label>
                            </p>
                        <?php endforeach;?>
			</div>
			<?php endif;?>
			<?php if(count($errorMessageArray) > 0):?>
			<div class="pageerrorcontainer">
			<?php foreach($errorMessageArray as $erroValue):?>
				<div class="pageoverflow">
					<img src="images/icons/system/stop.gif" class="systemicon" alt="Error" title="Error" />
					<label class="errorlabel"><?php echo $erroValue;?></label>
				</div>
			<?php endforeach;?>
			</div>
			<?php endif;?>

			<div class="pagecontainer">
                    <div class="pageoverflow">
					<div class="pageheader">Dealer Listing</div>
				</div>
					<div id="contentlist">
						<div class="pageoverflow">
							<p class="pageoptions">
								<a href="user.php" class="pageoptions">
									<img src="images/icons/system/newobject.gif" class="systemicon" alt="Add New Dealer" title="Add New Dealer" />
								</a>
								<a class="pageoptions" href="user.php">Add New Dealer</a>
							</p>
						</div>
                              <form action="userlisting.php" method="post" id="userlistingfrm">
                                   <div class="pageoptions">
                                        <div style="margin-top: 0; text-align: right">
                                             Search With Selected:&nbsp;&nbsp;
                                             <select name="searchtype" id="searchtype">
                                                  <option value="">All</option>
						  <option value="username" <?php echo ($searchtype == 'username')?'selected':''; ?> >Profile Name</option>
                                                  <option value="firstname" <?php echo ($searchtype == 'firstname')?'selected':''; ?> >First Name</option>
                                                  <option value="lastname" <?php echo ($searchtype == 'lastname')?'selected':''; ?> >Last Name</option>
                                                  <option value="email" <?php echo ($searchtype == 'email')?'selected':''; ?> >Email</option>
                                             </select>
                                             <span id="searchtext-container" class="display-none search-type">
                                                  <input type="text" class="search" name="searchtext" id="searchtext" value="<?php echo $searchtext;?>" />
                                             </span>
                                             <input type="submit" value="Submit" name="submit"/>
                                             <input type="submit"  id="show_all_button" value="Show All" name="showall"/>
                                        </div>
                                   </div>
                                    <table cellspacing="0" class="pagetable tablesorter" id="userlisting-table">
                                    <thead>
                                    <tr>
                                            <th title="Select All/Select None" class="checkbox" width="1%">
                                                    <input id="selectall" type="checkbox"/>
                                            </th>
					    <th width="23%" title="Profile Name">Dealer profile Name</th>
                                            <th width="20%" title="First Name">First Name</th>
                                            <th width="20%" title="Last Name">Last Name</th>
                                            <th width="20%" title="Email">Email</th>											<th width="20%" title="orders">Orders</th>											<th class="center-th" width="5%">Status</th>
                                            <th class="center-th" width="5%">Edit</th>
											<th class="center-th" width="18%">Delete</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php if(count($userData) > 0):?>
                                    <?php $counter = 0;?>
                                    <?php foreach($userData as $key => $value) :?>
                                    <?php if ( ($counter%2 ) == '0') : ?>
                                    <tr class="row1" onmouseout="this.className='row1';" onmouseover="this.className='row1hover';" >
                                    <?php  else :?>
                                    <tr class="row2" onmouseout="this.className='row2';" onmouseover="this.className='row2hover';">
                                    <?php endif;?>
                                    <td class="checkbox" >
                                    <input type="checkbox" class="content-checkbox" name="multicheck[]" value="<?php echo $value['userid'];?>"/>
                                    </td>
                                    <td><a href="user.php?id=<?php echo $value['userid']?>"><?php echo $value['username']?></a></td>
                                    <td><?php echo $value['firstname']?></td>
                                    <td><?php echo $value['lastname']?></td>
                                    <td><?php echo $value['email']?></td>																		<td class="pagepos">									<a href="orderlisting.php?userid=<?php echo $value['userid']?>"><img src="images/icons/system/small_order.png" class="systemicon" alt="Edit" title="Edit" /></a>									</td>																											<td class="pagepos">										<?php if($value['status'] == 'Active'): ?>											<a href="user.php?id=<?php echo $value['userid']?>&mode=status&type=active">											<div class="display-none"><?php echo $value['status']; ?></div>											<img src="images/icons/system/true.gif" class="systemicon" alt="Status" title="Active" />											</a>										<?php else: ?>											<div class="display-none"><?php echo $value['status']; ?></div>											<a href="user.php?id=<?php echo $value['userid']?>&mode=status&type=inactive">											<img src="images/icons/system/false.gif" class="systemicon" alt="Status" title="Inactive" />											</a>										<?php endif; ?>									</td>									
									<td class="pagepos">                                    	<a href="user.php?id=<?php echo $value['userid']?>"><img src="images/icons/system/edit.gif" class="systemicon" alt="Edit" title="Edit" /></a>                                    </td>									<td class="pagepos">
										<a href="user.php?id=<?php echo $value['userid']?>&mode=delete">
											<img src="images/icons/system/false.gif" class="systemicon" alt="Status" title="Delete" />
										</a>
									</td>
									
									
									
									
                                    </tr>
                                    <?php $counter++; ?>
                                    <?php endforeach;?>
                                    <?php else:?>
                                    <tr class="row2" onmouseout="this.className='row2';" onmouseover="this.className='row2hover';">
                                    <td>&nbsp;</td>
                                    <td colspan="7">No Records Found</td>
                                    </tr>
                                    <?php endif;?>
                                    </tbody>
                                    </table>
	                              <div class="pageoptions">
	                                   <div style="margin-top: 0; float: right; text-align: right">
	                                        With Selected:&nbsp;&nbsp;
	                                        <select name="multiaction" id="multiaction">
                                                  <option value="">none</option>
                                                  <option value="delete" <?php echo ($action == 'delete')?'selected':''; ?> >Delete</option>
                                                </select>
	                                        <input type="submit" accesskey="s" value="Submit"/>

                                            </div>
                                    </div>
                                   <?php if($totalUser > 0):?>
                                   <div class="paging">
	                                   <div>
	                                        <?php echo $paginator->displayPages(); ?>
								</div>
							</div>
                                   <div class="clearb"></div>
                                   <br/><br/>
                                   <?php endif;?>
	                              <div style="float: left;">
	                                   <a href="user.php" class="pageoptions">
	                                        <img src="images/icons/system/newobject.gif" class="systemicon" alt="Add New Dealer" title="Add New Dealer" />
                                           </a>
                                            <a class="pageoptions" href="user.php">Add New Dealer</a>
                                      </div>
	                              <br />
	                              <div class="clearb"></div>
                         </form>
				</div>
			</div>
               <p class="pageback">
				<a class="pageback" href="dashboard.php">&#171; Back to Home</a>
			</p>
               <div class="clearb"></div>
		</div>
     </div>
     <?php include('templates/footer.php');?>
</body>
</html>